/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.StudentDaoImpl;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

/**
 *
 * @author pluto
 */
class BarChart {
    //1-写一个ChartPanel变量

    ChartPanel jframe;

    //2-BarChart的无参数的构造方法
    public BarChart(String math, String java, String sport) {
        DefaultCategoryDataset data = new DefaultCategoryDataset();
        data.setValue(Double.parseDouble(math), "数学", "数学");
        data.setValue(Double.parseDouble(java), "Java", "Java");
        data.setValue(Double.parseDouble(sport), "体育", "体育");
        JFreeChart chart = ChartFactory.createBarChart3D(
                "该生的成绩柱状图", //图表标题
                "课程",//目录轴的显示标签 
                "分数",//数值轴的显示标签 
                data,
                PlotOrientation.VERTICAL, //图表方向 水平 垂直
                true, //是否显示图例(对于简单的图表建议显示图例)
                false,//是否生成工具 
                false);  //是否生成网址链接
        //字体设置
        //获得图表区域对象
        CategoryPlot plot = chart.getCategoryPlot();
        //水平底部列表 
        CategoryAxis domain = plot.getDomainAxis();
        //垂直标题字体设置

        domain.setTickLabelFont(new Font("黑体", Font.BOLD, 16));
        //水平底部标题设置
        domain.setLabelFont(new Font("黑体", Font.BOLD, 20));
        //获取柱状体
        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 16));
        //设置lengend字体
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 16));
        chart.getTitle().setFont(new Font("黑体", Font.BOLD, 16));
        //初始化Jframe
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelFont(new Font("黑体", Font.PLAIN, 20));
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
        renderer.setItemLabelAnchorOffset(10D);
        jframe = new ChartPanel(chart);

    }

    //4-返回一个ChartPanel
    public ChartPanel getPanel() {
        return jframe;
    }

}

public class QueryJFrame extends javax.swing.JFrame {

    /**
     * Creates new form QueryJFrame
     */
    
    StudentDaoImpl sdi = new StudentDaoImpl();
    static QueryJFrame qJF;
    int i = 0;
    public QueryJFrame() {
        this.setTitle("查询学生成绩");
        this.setResizable(false);
        StudentDaoImpl sdi=new StudentDaoImpl();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        //this.setVisible(true);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idJPanel = new javax.swing.JPanel();
        idJLabel = new javax.swing.JLabel();
        inputJTextField = new javax.swing.JTextField();
        queryJButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        resultJpanel = new javax.swing.JPanel();
        resultPane1 = new javax.swing.JScrollPane();
        resultTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        idJLabel.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        idJLabel.setText("输入学号:");

        inputJTextField.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

        queryJButton.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        queryJButton.setText("查询");
        queryJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                queryJButtonActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jButton1.setText("生成成绩图");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout idJPanelLayout = new javax.swing.GroupLayout(idJPanel);
        idJPanel.setLayout(idJPanelLayout);
        idJPanelLayout.setHorizontalGroup(
            idJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(idJPanelLayout.createSequentialGroup()
                .addComponent(idJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inputJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(queryJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(0, 12, Short.MAX_VALUE))
        );
        idJPanelLayout.setVerticalGroup(
            idJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(idJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(idJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(inputJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(queryJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        resultJpanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultJpanelMouseClicked(evt);
            }
        });

        resultTextArea1.setColumns(20);
        resultTextArea1.setFont(new java.awt.Font("Monospaced", 0, 20)); // NOI18N
        resultTextArea1.setRows(5);
        resultPane1.setViewportView(resultTextArea1);

        jButton2.setText("返回");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout resultJpanelLayout = new javax.swing.GroupLayout(resultJpanel);
        resultJpanel.setLayout(resultJpanelLayout);
        resultJpanelLayout.setHorizontalGroup(
            resultJpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(resultPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
            .addGroup(resultJpanelLayout.createSequentialGroup()
                .addGap(231, 231, 231)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        resultJpanelLayout.setVerticalGroup(
            resultJpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(resultJpanelLayout.createSequentialGroup()
                .addComponent(resultPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(idJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(resultJpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(idJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resultJpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void queryJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_queryJButtonActionPerformed
        // TODO add your handling code here:
        String input_id = inputJTextField.getText();
        sdi.queryById(resultTextArea1,input_id);

    }//GEN-LAST:event_queryJButtonActionPerformed

    private void resultJpanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultJpanelMouseClicked
        // TODO add your handling code here:

    }//GEN-LAST:event_resultJpanelMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // TODO add your handling code here:
        
        String input_id = inputJTextField.getText();
        sdi.getBarChart(input_id);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        qJF.setVisible(false);
        //SystemJFrame sJf = new SystemJFrame();
        //sJf.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

       
    
    public static void start(){
        qJF = new QueryJFrame();
        qJF.setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(QueryJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(QueryJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(QueryJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(QueryJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new QueryJFrame().setVisible(true);
//                
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel idJLabel;
    private javax.swing.JPanel idJPanel;
    private javax.swing.JTextField inputJTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton queryJButton;
    private javax.swing.JPanel resultJpanel;
    private javax.swing.JScrollPane resultPane1;
    private javax.swing.JTextArea resultTextArea1;
    // End of variables declaration//GEN-END:variables

}
